//
//  FKAreaAndTradeView.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/20.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKAreaAndTradeView.h"
#import "FKAlignmentLayout.h"
#import "FKAreaItemCell.h"
#import "FKTradeBtn.h"
#import "FKTrade.h"
static NSString *const areaId = @"FKAreaItemCell";

@interface FKAreaAndTradeView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *datas;

@property (nonatomic, weak) UIView *bottomView;
@property (nonatomic, strong) NSArray *tradeBtns;
@property (nonatomic, strong) NSMutableDictionary *param;

@end

@implementation FKAreaAndTradeView

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
    }
    return _param;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    self.clipsToBounds = true;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    CGFloat itemW = fkScreenW / 5.f;
    CGFloat itemH = fk_adjustW(20);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
//    layout.itemSize = CGSizeMake(itemW, itemH);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.contentInset = UIEdgeInsetsMake(fk_adjustW(12), fk_adjustW(58), 0, 12);
    [collectionView registerClass:[FKAreaItemCell class] forCellWithReuseIdentifier:areaId];
    collectionView.allowsMultipleSelection = true;
    
    UILabel *areaTitleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor666666 text:@"区域:"];
    [self addSubview:areaTitleLabel];
    
    UIView *bottomView = [[UIView alloc] init];
    [self addSubview:bottomView];
    self.bottomView = bottomView;
    
    UILabel *tradeTitleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor666666 text:@"热门商圈:"];
    [bottomView addSubview:tradeTitleLabel];
    
    NSMutableArray *tradeBtns = [NSMutableArray array];
    for (NSInteger index = 0; index < 20; index++) {
        FKTradeBtn *tradeBtn = [FKTradeBtn fk_btnWithTarget:self action:@selector(tradeBtnClicked:) font:fk_adjustFont(14) normalTitle:nil normalTitleColor:fkColor666666 selectedTitle:nil selectedTitleColor:fkMainColor normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
        [bottomView addSubview:tradeBtn];
        [tradeBtns addObject:tradeBtn];
        [tradeBtn fk_viewCornerRadius:2 borderWidth:0 borderColor:nil];
        tradeBtn.backgroundColor = fkColorECECEC;
    }
    self.tradeBtns = tradeBtns;
    
    UIView *separator = [[UIView alloc] init];
    [self addSubview:separator];
    separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    [areaTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.top.offset(fk_adjustW(12));
        make.height.offset(itemH);
    }];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(0.8);
    }];
    [tradeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.top.offset(0);
        make.height.offset(fk_adjustW(20));
    }];
}

- (void)tradeBtnClicked:(FKTradeBtn *)btn
{
    if (btn.isSelected) {
        btn.selected = false;
        self.param[@"item_trade_id"] = @"";
        self.param[@"trade_id"] = @"";
    } else {
        for (FKTradeBtn *itemBtn in self.tradeBtns) {
            if ([btn isEqual:itemBtn]) {
                itemBtn.selected = true;
                if (self.isFastShare) {
//                    self.param[@"region_id"] = @"";
                    self.param[@"item_trade_id"] = itemBtn.trade.id;
                } else {
//                self.param[@"area_id"] = @"";
                    self.param[@"trade_id"] = itemBtn.trade.id;
                }
            } else {
                itemBtn.selected = false;
            }
        }
    }
    if (self.didSelectedTrade) {
        self.didSelectedTrade(self.param);
    }
}

- (void)setData:(id)data
{
    _data = data;
    
    self.datas = data[@"areas"];
    [self.collectionView reloadData];
    
    NSArray *trades = data[@"trades"];
    for (NSInteger index = 0; index < self.tradeBtns.count; index++) {
        FKTradeBtn *itemBtn = self.tradeBtns[index];
        if (index < trades.count) {
            FKTrade *trade = trades[index];
            itemBtn.frame = trade.itemFrame;
            itemBtn.trade = trade;
        } else {
            itemBtn.frame = CGRectZero;
        }
    }
    
    CGFloat areaH = [data[@"areaH"] floatValue];
    CGFloat headerH = [data[@"headerH"] floatValue];
    self.bottomView.frame = CGRectMake(0, areaH, fkScreenW, headerH - areaH);
}

- (void)updateTrades:(NSArray *)trades tradeH:(CGFloat)tradeH
{
    for (NSInteger index = 0; index < self.tradeBtns.count; index++) {
        FKTradeBtn *itemBtn = self.tradeBtns[index];
        if (index < trades.count) {
            FKTrade *trade = trades[index];
            itemBtn.frame = trade.itemFrame;
            itemBtn.trade = trade;
        } else {
            itemBtn.frame = CGRectZero;
        }
    }
    
    CGFloat areaH = [self.data[@"areaH"] floatValue];
    CGFloat headerH = areaH + tradeH;
    self.fk_height = headerH;
    self.bottomView.frame = CGRectMake(0, areaH, fkScreenW, tradeH);
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:areaId forIndexPath:indexPath];
    NSDictionary *item = self.datas[indexPath.item];
    cell.data = item[@"data"];
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *item = self.datas[indexPath.item];
    return [item[@"itemSize"] CGSizeValue];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消其他所有cell的选中
    NSDictionary *item = self.datas[indexPath.item];
    for (NSInteger index = 0; index < self.datas.count; index++) {
        if (indexPath.item != index) {
            NSIndexPath *otherIndexPath = [NSIndexPath indexPathForItem:index inSection:0];
            [collectionView deselectItemAtIndexPath:otherIndexPath animated:false];
        }
    }
    
//    NSDictionary *item = self.datas[indexPath.item];
    if (self.isFastShare) {
        self.param[@"region_id"] = item[@"data"][@"id"];
        self.param[@"item_trade_id"] = @"";
    } else {
        self.param[@"area_id"] = item[@"data"][@"id"];
        self.param[@"trade_id"] = @"";
    }
//    if (self.didSelectedItem) {
//        self.didSelectedItem(self.param);
//    }
    if (self.didSelectedArea) {
        self.didSelectedArea(self.param);
    }
    
    for (FKTradeBtn *btn in self.tradeBtns) {
        btn.selected = false;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isFastShare) {
        self.param[@"region_id"] = @"";
        self.param[@"item_trade_id"] = @"";
    } else {
        self.param[@"area_id"] = @"";
        self.param[@"trade_id"] = @"";
    }
    
    if (self.didSelectedArea) {
        self.didSelectedArea(self.param);
    }
    
    for (FKTradeBtn *btn in self.tradeBtns) {
        btn.selected = false;
    }
}


@end
